package com.jy.bo.common.conf.util;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @program: binary-option
 * @description
 * @author: binary
 * @create: 2020-06-05 17:59
 **/
public class DateUtils {


    /**
     * 增加秒
     * @param date
     * @param second
     * @return
     */
    public static Date addDate(Date date, int second) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + (long)second * 1000L);
        return c.getTime();
    }

    public static long getMillis(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }

    /**
     * 是否在某一时间段内
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean isTimeIn(Date nowTime, Date beginTime, Date endTime) {
        return nowTime.getTime() >= beginTime.getTime()
                && nowTime.getTime() <= endTime.getTime();
    }

    public static boolean isTimeIn(Date time, String begin, String end) {
        return isTimeIn(time, getDate(begin, "HH:mm:ss"), getDate(end, "HH:mm:ss"));
    }

    /**
     * 把传入的日期字符串，转换成指定格式的日期对象
     * @param dateString 日期字符串
     * @param pattern    指定转换格式
     * @return 日期对象
     */
    public static Date getDate(String dateString, String pattern) {
        SimpleDateFormat df = null;
        Date date =null;
        if (!StringUtils.isBlank(dateString)) {
            try {
                df = new SimpleDateFormat(pattern);
                date = df.parse(dateString);

            } catch (Exception e) {

            }
        }
        return date;
    }


    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime,
                                         Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isInTime(String openingTime, String closingTime){
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");// 设置日期格式
        Date now = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            now = df.parse(df.format(new Date()));
            beginTime = df.parse(openingTime);
            endTime = df.parse(closingTime);
        } catch (Exception e) {

        }
        if (!DateUtils.belongCalendar(now, beginTime, endTime)) {
            return false;
        }
        return true;
    }


    public static boolean isWeekend(Date date) {

        Calendar cal = Calendar.getInstance();
//        TimeZone tz = TimeZone.getTimeZone("Asia/Kolkata");
//        cal.setTimeZone(tz);
        cal.setTime(date);
        if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            return true;
        } else{
            return false;
        }
    }

    public static Date getLastWeekend(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);//将date - 7
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
        return d;
    }
    public static Date getNextWeekend(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);//将date + 7
        c.add(Calendar.DATE, + 7);
        Date d = c.getTime();
        return d;
    }

    public static void main(String[] args) {
        TimeZone tz = TimeZone.getTimeZone("Asia/Kolkata");
        System.out.println(tz);
        System.out.println(isWeekend(new Date()));
    }
}
